var setting = {
	data: {
		simpleData: {
			enable: true,
			idKey: "id",
			pIdKey: "parentId",
			rootPId: -1
		},
		key: {
			url:"nourl"
		}
	}
};
var ztree;

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: 0,
		title: null,
		myFile: {},
		dialogVisible: false,
	},
	methods: {
		updateParentId(){
			$.ajax({
				type: "POST",
				url: baseURL + 'myCourse/myfile/updateParentId',
				contentType: "application/json",
				data: JSON.stringify(vm.myFile),
				success: function(r){
					if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();

						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		handleRemove(file, fileList) {
			console.log(file, fileList);
		},
		handlePictureCardPreview(file) {
			// this.myFile.filePath = file.url;
			this.dialogVisible = true;
		},
		uploadSuccess(response, file, fileList	){
			console.log(response);
			console.log(file);
			console.log(fileList);
			vm.myFile.filePath = response.returnUrl
			vm.dialogVisible = true;
		},
		menuTree: function(){
			layer.open({
				type: 1,
				offset: '50px',
				skin: 'layui-layer-molv',
				title: "选择菜单",
				area: ['300px', '450px'],
				shade: 0,
				shadeClose: false,
				content: jQuery("#menuLayer"),
				btn: ['确定', '取消'],
				btn1: function (index) {
					var node = ztree.getSelectedNodes();
					//选择上级菜单
					vm.myFile.parentId = node[0].id;
					vm.myFile.parentName = node[0].name;
					console.log(vm.myFile.parentId)
					console.log(vm.myFile.parentName)
					if(vm.myFile.id != null){
						vm.updateParentId()

					}
					layer.close(index);
				}
			});
		},
		clearFiles:function(){
			vm.$refs.upload.clearFiles();
		},
		getMenu: function(menuId){
			//加载菜单树
			$.get(baseURL + "myCourse/myfile/getMyFilemenuTree", function(r){
				ztree = $.fn.zTree.init($("#menuTree"), setting, r);
				// var node = ztree.getNodeByParam("id", vm.menu.parentId);
				// ztree.selectNode(node);
				//
				// vm.menu.parentName = node.name;
			})
		},
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.myFile = {parentId:null,parentName:null,filePath:null,type:1};
			vm.getMenu()
		},
		addMenu: function(){
			vm.showList = 1;
			vm.myFile = {parentId:null,parentName:null,filePath:null,type:0};
			vm.getMenu()
		},
		addFile: function(){
			vm.showList = 2;
			vm.myFile = {parentId:null,parentName:null,filePath:null,type:1};
			vm.getMenu()
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.myFile.id == null ? "myCourse/myfile/save" : "myCourse/myfile/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.myFile),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();

						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (id) {
			// var ids = getSelectedRows();
			// if(ids == null){
			// 	return ;
			// }
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "myCourse/myfile/delete",
                    contentType: "application/json",
				    data: JSON.stringify(id),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								vm.reload()
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "myCourse/myfile/info/"+id, function(r){
                vm.myFile = r.myFile;
            });
		},
		reload: function (event) {
			vm.showList = 0;
			table.table.refresh();
			vm.getMenu()
			vm.$refs.upload.clearFiles();
		}
	}
});



var table = {
	id: "table",
	table: null,
	layerIndex: -1
};


/**
 * 初始化表格的列
 */
table.initColumn = function () {
	var columns = [
		{field: 'selectItem', radio: true},
		{title: '菜单ID', field: 'id', visible: false, align: 'center', valign: 'middle', width: '80px'},
		{title: '菜单名称', field: 'name', align: 'center', valign: 'middle', sortable: true, width: '100px'},
		{title: '类型', field: 'type', align: 'center', valign: 'middle', sortable: true, width: '40px', formatter: function(item, index){
				if(item.type === 0){
					return '<span class="label label-primary">目录</span>';
				}
				if(item.type === 1){
					return '<span class="label label-success">文件</span>';
				}
			}},
		{title: '地址', field: 'filePath', align: 'center', valign: 'middle', sortable: true, width: '100px',formatter: function(item, index){
		    if(item.type === 1)
                return '<button class="btn btn-info btn-sm" onclick="copyPath(\'http://'+item.filePath+'\')" >复制链接</button>';
            }},
		{title: '操作', field: 'id', align: 'center', valign: 'middle', sortable: true, width: '100px', formatter: function(item, index){
				return '<button class="btn btn-default btn-sm" onclick="updeteItem('+item.id+')" >修改</button>'
				+ '<button class="btn btn-danger btn-sm" onclick="delITem('+item.id+')" >删除</button>';
			}},
		]
	return columns;
};

function delITem(id){
	vm.del(id)
}

function updeteItem(id){
	vm.myFile.id=id
	vm.menuTree(id)
}


function copyPath(filePath){
    var Url2=filePath
    var thisFilePath = Url2;
    var s = thisFilePath.substring(0,4)
    if(s != 'http'){
		Url2 = 'http://'+Url2
    }
    var oInput = document.createElement('input');
    oInput.value = Url2;
    document.body.appendChild(oInput);
    oInput.select(); // 选择对象
    document.execCommand("Copy"); // 执行浏览器复制命令
    oInput.className = 'oInput';
    oInput.style.display='none';
    alert('复制成功');


}






function getMenuId () {
	var selected = $('#table').bootstrapTreeTable('getSelections');
	if (selected.length == 0) {
		alert("请选择一条记录");
		return null;
	} else {
		return selected[0].id;
	}
}


$(function () {
	var colunms = table.initColumn();
	vm.getMenu()
	var t = new TreeTable(table.id, baseURL + "myCourse/myfile/list", colunms);
	t.setExpandColumn(2);
	t.setIdField("id");
	t.setCodeField("id");
	t.setParentCodeField("parentId");
	t.setExpandAll(false);
	t.init();
	table.table = t;
});